// 不足位数前面补0
function PrefixInteger(num, length) {
	return (Array(length).join('0') + num).slice(-length)
}

function formatDttm(timestamp) {
	if (!timestamp) return '';
	if (timestamp) {
		let date = new Date(timestamp);
		let year = date.getFullYear();
		let moth = date.getMonth() + 1;
		let day = date.getDate();
		let h = date.getHours();
		let m = date.getMinutes();
		let s = date.getSeconds();

		return year + '-' + PrefixInteger(moth, 2) + '-' + PrefixInteger(day, 2) + ' ' + PrefixInteger(h, 2) + ':' +
			PrefixInteger(m, 2) + ':' + PrefixInteger(s, 2)
	}
}

function formatYmdhm(timestamp) {
	if (!timestamp) return '';
	if (timestamp) {
		let date = new Date(timestamp);
		let year = date.getFullYear();
		let moth = date.getMonth() + 1;
		let day = date.getDate();
		let h = date.getHours();
		let m = date.getMinutes();
		let s = date.getSeconds();

		return year + '-' + PrefixInteger(moth, 2) + '-' + PrefixInteger(day, 2) + ' ' + PrefixInteger(h, 2) + ':' +
			PrefixInteger(m, 2)
	}
}

function formatHm(timestamp) {
	if (!timestamp) return '';
	if (timestamp) {
		let date = new Date(timestamp);
		let h = date.getHours();
		let m = date.getMinutes();

		return PrefixInteger(h, 2) + ':' +
			PrefixInteger(m, 2)
	}
}

function formatDt(time) {
	if (!time) return '';
	let date = new Date(time);
	let year = date.getFullYear();
	let moth = date.getMonth() + 1;
	let day = date.getDate();
	return year + '-' + PrefixInteger(moth, 2) + '-' + PrefixInteger(day, 2)
}

function formatNow(time) {
	if (!time) return ' ';
	let date = new Date(time);
	let year = date.getFullYear();
	let moth = date.getMonth() + 1;
	let day = date.getDate();
	let h = date.getHours();
	let m = date.getMinutes();

	let now = new Date();
	let now_year = now.getFullYear();
	let now_moth = now.getMonth() + 1;
	let now_day = now.getDate();

	if (year == now_year && moth == now_moth) {
		if (day == now_day) {
			return '今天' + PrefixInteger(h, 2) + ':' + PrefixInteger(m, 2)
		}
		if (day == (now_day + 1)) {
			return '明天' + PrefixInteger(h, 2) + ':' + PrefixInteger(m, 2)
		}
		if (day == (now_day + 2)) {
			return '后天' + PrefixInteger(h, 2) + ':' + PrefixInteger(m, 2)
		}
	}
	return year + '-' + PrefixInteger(moth, 2) + '-' + PrefixInteger(day, 2) + ' ' + PrefixInteger(h, 2) + ':' +
		PrefixInteger(m, 2)
}

export {
	formatDttm,
	formatYmdhm,
	formatHm,
	formatDt,
	formatNow,
}
